<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//广告设置，首页banner焦点图

class Adv extends Admin_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('admin/adv_model');

    }
    //广告信息
    public function index()
    {
        $data["totals"] = $this->adv_model->count_adv();
        $data['advs'] = $this->adv_model->list_adv();
        $this->load->view('adv-index.html',$data);
    }

    #显示添加广告页面
    public function add(){
        $this->load->view('adv-add.html');
    }

    //添加广告数据处理
    public function insert(){
        $config['upload_path'] = './public/banner/';
        if(!file_exists('./public/banner')){
            mkdir('./public/banner',0777,true);//存储路径
        }
        #完成上传图片
        $config['allowed_types'] = 'gif|jpg|png|jpeg|x-png|pjpeg';
        $config['max_width']  = '3000';
        $config['overwrite']  = FALSE;
        $config['encrypt_name']  = TRUE; //开启重命名
        $this->load->library('upload',$config);
        #配置上传相关参数
        if ($this->upload->do_upload('adv_img')) {
            # 上传成功，获取文件名
            $fileinfo = $this->upload->data();
            $data['adv_img'] = $fileinfo['file_name'];
            #获取表单提交数据
            $data['adv_name'] = $this->input->post('adv_name');
            $data['adv_url'] = $this->input->post('adv_url');
            $data['sort_order'] = $this->input->post('sort_order');
            $data['is_show'] = $this->input->post('is_show');

            #调用广告模型完成插入动作
            if ($this->adv_model->add_adv($data)) {
                $data['message'] = '添加广告成功';
                $data['wait'] = 1111123;
                $data['url'] = site_url('admin/adv/index');
                $this->load->view('message.html',$data);
            } else {
                $data['message'] = '添加广告失败';
                $data['wait'] = 111123;
                $data['url'] = site_url('admin/adv/add');
                $this->load->view('message.html',$data);
            }
        } else {
            # 上传失败
            $data['message'] = $this->upload->display_errors();
            $data['wait'] = 1111123;
            $data['url'] = site_url('admin/adv/add');
            $this->load->view('message.html',$data);
        }
    }



}